$(function () {
    var carinfor_datagrid = $("#carinfor_datagrid");
    var carinfor_form =$("#carinfor_form");
    var carinfor_dialog =  $("#carinfor_dialog");

    var methObject = {
        add: function () {
            $("#password_tr").show();
            carinfor_form.form("clear");
            carinfor_dialog.dialog("setTitle", "新增操作");
            carinfor_dialog.dialog("open");
        },

        edit: function () {
            //row底层为null,通过null进行下一步操作
            var row = carinfor_datagrid.datagrid("getSelected");
            //未选中时
            if (!row) {
                $.messager.alert('温馨提示', "请选择一条数据", "warning");
                return;
            }

            $("#password_tr").hide("slow");
            carinfor_form.form("clear");

            //数据进行回显操作
            carinfor_form.form("load", row);

            carinfor_dialog.dialog("setTitle", "编辑操作");
            carinfor_dialog.dialog("open");
        },

        cancel: function () {
            carinfor_dialog.dialog("close");
        },

        brush:function () {
            carinfor_datagrid.datagrid("reload");
        },

        save: function () {
            carinfor_form.form("submit", {
                url: '/carinformation/saveOrUpdate.do',
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', data.msg, 'info', function () {
                            carinfor_datagrid.datagrid("reload");
                            carinfor_dialog.dialog("close");
                        })
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })

        },
        changeState:function () {
          var row =  carinfor_datagrid.datagrid("getSelected");
          if(!row){
              $.messager.alert('温馨提示',"请选择一条数据","warning");
              return;
          }
          $.get("/carinformation/changeState.do",{id:row.id},function (data) {
              if(data.success){
                  $.messager.alert('温馨提示',data.msg,"info",function () {
                      carinfor_datagrid.datagrid("reload")
                  });
              }else {
                  $.messager.alert('温馨提示', data.msg, 'error');
              }
          },"json")
        },
        query:function () {
       var keyword =  $("#keyword").textbox("getValue");
       carinfor_datagrid.datagrid("load",{
           keyword:keyword
       })
        }
//高级查询
    }
    $("[data-cmd]").click(function() {
        var cmd = $(this).data("cmd");
        methObject[cmd]();
    })



    carinfor_datagrid.datagrid({
        url: '/carinformation/query.do',
        fit: true,
        fitColumns: true,
        pagination: true,
        striped: true,
        singleSelect:true,
        columns: [[
            {field: 'name', title: '车主姓名', width: 100},
            {field: 'brand', title: '车辆品牌', width: 100},
            {field: 'type', title: '车辆类型', width: 100},
            {field: 'carNumber', title: '车辆号码', width: 100},
            {field: 'airVolume', title: '排气量/L', width: 100},
            {field: 'inputTime', title: '购买日期', width: 100},
            {field: 'evaluation', title: '市场估价/万元', width: 100},
            {field: 'state', title: '分类', width: 100,formatter:function (value,row,index) {
                console.log(row);
                return value ?"<font color='green'>载物</font>":"<font color='red'>载客</font>"
            }},
            {field: 'carType', title: '车载大小', width: 100},
            {field: 'remarks', title: '备注信息', width: 100}
        ]],
        toolbar: '#tb',
        onClickRow:function (index,row) {
            if(!row.state){
                $("#changState_btn").linkbutton({
                   text:'载物'
                });
            }else {
                $("#changState_btn").linkbutton({
                   text:'载客'
                })
            }
        }
    })
    carinfor_dialog.dialog({
        width: 300,
        height: 460,
        closed: true,
        buttons: '#bt'
    })
})


